package com.lianniu.mgr.cms.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.model.MainNavigationVo;
import com.lianniu.mgr.cms.service.IMainNavigationService;
import com.lianniu.mgr.pagination.PaginationInfo;

@Controller
@RequestMapping("/rest/mainnavigation")
public class MainNavigationRestController {

private final static Logger logger = Logger.getLogger(MainNavigationRestController.class);
	
	@Autowired
	private IMainNavigationService mainNavigationService;
	
	@RequestMapping(value="/MainMainNavigationList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult MainMainNavigationList(HttpServletRequest request, MainNavigationVo curVo, PaginationInfo paginationInfo)
	 {
	       logger.info("MainNavigationRestController  MainNavigationList begin.");
	       try {
	           
	           logger.info("MainNavigationList begin.");          
	           BaseResult baseResult = mainNavigationService.selectByMainNavigationForPage(curVo, paginationInfo);
	           logger.info("MainNavigationList end.");
	           logger.info("MainNavigationRestController MainNavigationList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("MainNavigationRestController MainNavigationList error:" + e.getMessage());
	       logger.error("MainNavigationRestController MainNavigationList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	
	@RequestMapping(value = {"/MainNavigationAdd"}, method = RequestMethod.POST)
	public @ResponseBody BaseResult MainNavigationAdd(HttpServletRequest request,MainNavigationVo curVo) {
	      logger.info("MainNavigationRestController MainNavigationAdd begin.");      
	      logger.info("MainNavigationRestController MainNavigationAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("MainNavigationAdd begin.");
	        
	          BaseResult baseResult = mainNavigationService.insertMainNavigation(curVo);
	          logger.info("MainNavigationAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("MainNavigationAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("MainNavigationAdd error:"+e.getMessage());
	      logger.info("MainNavigationRestController MainNavigationAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	
	 
	@RequestMapping(value = {"/MainMainNavigationEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult MainMainNavigationEdit(HttpServletRequest request,MainNavigationVo curVo) {
	      logger.info("MainNavigationRestController MainNavigationEdit begin.");      
	      logger.info("MainNavigationRestController MainNavigationEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("MainNavigationEdit begin.");
	          BaseResult baseResult = mainNavigationService.updateById(curVo);
	          logger.info("MainNavigationEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("MainNavigationEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("MainNavigationRestController error:"+e.getMessage());
	      logger.info("MainNavigationRestController MainNavigationEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }
	  
	
	 @RequestMapping(value="/selectMainNavigationById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectMainNavigationById(HttpServletRequest request,MainNavigationVo curVo) {
	        logger.info("MainNavigationRestController selectMainNavigationById begin.");
	        try {
	                    
	          BaseResult baseResult = mainNavigationService.selectById(curVo);
	          logger.info("MainNavigationRestController end.");
	          logger.info("MainNavigationRestController selectMainNavigationById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("MainNavigationRestController selectMainNavigationById error:" + e.getMessage());
	          logger.error("MainNavigationRestController selectMainNavigationById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/MainNavigationDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult MainNavigationDel(HttpServletRequest request) {
	      logger.info("MainNavigationRestController MainNavigationDel begin.");
	     try {          
	          logger.info("MainNavigationDel begin.");
	          BaseResult baseResult = mainNavigationService.deleteMainNavigationBatch(request.getParameter("ids").split(","));		
	          logger.info("MainNavigationDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("MainNavigationDel end.");
	          
	          logger.info("MainNavigationRestController MainNavigationDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("MainNavigationRestController MainNavigationDel error:" + e.getMessage());
	      logger.info("MainNavigationRestController MainNavigationDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	 
	 @RequestMapping(value = {"/checkMsSortName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkMsSortName(HttpServletRequest request) {
	      Boolean flag=true;
	      String msSortName=  request.getParameter("msSortName");
	      logger.info("MainNavigationRestController checkMsSortName begin.");
	     try {          
	          logger.info("checkMsSortName begin.");
	          if("".equalsIgnoreCase(msSortName)||msSortName==null)
	          {return flag;}
	          flag = mainNavigationService.checkMsSortName(msSortName);         
	          logger.info("checkMsSortName end.");
	          
	          logger.info("MainNavigationRestController checkMsSortName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("MainNavigationRestController checkMsSortName error:" + e.getMessage());
	      logger.info("MainNavigationRestController checkMsSortName end.");
	      return flag;
	    }

	  }
	 
	 @RequestMapping(value = {"/checkMsSortNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkMsSortNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String msSortName=  request.getParameter("msSortName");
	      String id=request.getParameter("id");
	      logger.info("MainNavigationRestController checkMsSortNameEdit begin.");
	     try {          
	          logger.info("checkMsSortNameEdit begin.");
	          MainNavigationVo vo=new MainNavigationVo();	         
	          vo.setId(id);
	          
	          BaseResult baseResult =mainNavigationService.selectById(vo);	          
	          MainNavigationVo curVo=(MainNavigationVo) baseResult.getData();
	          if(curVo.getId().equals(id)&&curVo.getMsSortName().equals(msSortName))
	          {return flag;}
	          if("".equalsIgnoreCase(msSortName)||msSortName==null)
	          {return flag;}
	          flag = mainNavigationService.checkMsSortName(msSortName);         
	          logger.info("checkMsSortNameEdit end.");
	          
	          logger.info("MainNavigationRestController checkMsSortNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("MainNavigationRestController checkMsSortNameEdit error:" + e.getMessage());
	      logger.info("MainNavigationRestController checkMsSortNameEdit end.");
	      return flag;
	    }

	  }
}
